package com.itestor.iptv.modules.ne.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.itestor.iptv.global.dao.GenericHibernateDao;
import com.itestor.iptv.modules.ne.domain.IptvLostRate;
import com.itestor.iptv.modules.ne.domain.IptvQuality;
import com.itestor.iptv.modules.ne.domain.RobotManage;
import com.itestor.iptv.modules.ne.vo.RobotNodeStatusBean;

/**
 * @author Mr.li
 */
public class RobotManageDao extends GenericHibernateDao<RobotManage, Integer> {

    /**
     * 
     * @param pId
     */
    public void deleteRelGroup(int pId) {
        String hql = "delete from RobotGroupManage t where t.idRobot=:idRobot";
        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("idRobot", pId);
        query.executeUpdate();
    }

    /**
     * @param pId
     */
    public void deleteRelRoleGroup(int pId) {
        String hql = "delete from RoleGroupRobot t where t.idRobot=:idRobot";
        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("idRobot", pId);
        query.executeUpdate();
    }

    /**
     * 获取用户节点状态列表
     * @param pUserId
     * @param pLastIpUpdateTime
     * @return Map
     */
    public Map<Integer, Map<Integer, RobotNodeStatusBean>> getNodeStatusListByUser(
            Integer pUserId, Date pLastIpUpdateTime) {
        Map<Integer, Map<Integer, RobotNodeStatusBean>> nodeStatusMap = new HashMap<Integer, Map<Integer, RobotNodeStatusBean>>();

        // 用户节点设备
        List<RobotNodeStatusBean> userNodeList = this
                .getNodeStatusListByUser(pUserId);

        // 用户在线设备
        Map<Integer, RobotNodeStatusBean> userOnLineNodeList = this
                .getOnLineNodeStatusListByUser(pUserId, pLastIpUpdateTime);

        Iterator<RobotNodeStatusBean> it = userNodeList.iterator();
        Map<Integer, RobotNodeStatusBean> offerLineNodeMap = new HashMap<Integer, RobotNodeStatusBean>();

        while (it.hasNext()) {
            RobotNodeStatusBean bean = it.next();
            if (!userOnLineNodeList.containsKey(bean.getRobotManage()
                    .getIdRobot())) {
                offerLineNodeMap.put(bean.getRobotManage().getIdRobot(), bean);
            }

            it.remove();
        }

        // 获取离线设备列表
        nodeStatusMap.put(RobotNodeStatusBean.NODE_STATUS_OFFLINE,
                offerLineNodeMap);
        // 获取在线设备列表
        nodeStatusMap.put(RobotNodeStatusBean.NODE_STATUS_ONLINE,
                userOnLineNodeList);

        return nodeStatusMap;
    }

    /**
     * 获取用户在线节点设备列表
     * @param pUserId
     * @param pLastIpUpdateTime
     * @return List
     */
    @SuppressWarnings("unchecked")
    public Map<Integer, RobotNodeStatusBean> getOnLineNodeStatusListByUser(
            Integer pUserId, Date pLastIpUpdateTime) {
        String hql = "select new com.itestor.iptv.modules.ne.vo.RobotNodeStatusBean(r,n) "
                + " from RobotManage r,RobotNetInfo n,RoleGroupRobot t,UserRoleGroup g "
                + " where r.idRobot=n.idRobot and t.idRobot=r.idRobot "
                + " and t.idRoleGroup = g.idRoleGroup "
                + " and g.idUser=:userId and n.ipUpdateTime >:lastTime";

        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("userId", pUserId);
        query.setParameter("lastTime", new Timestamp(pLastIpUpdateTime
                .getTime()));

        Map<Integer, RobotNodeStatusBean> onLineNodesMap = new HashMap<Integer, RobotNodeStatusBean>();
        List<RobotNodeStatusBean> list = query.list();

        for (RobotNodeStatusBean bean : list) {
            onLineNodesMap.put(bean.getRobotManage().getIdRobot(), bean);
        }

        return onLineNodesMap;
    }

    /**
     * 获取用户节点设备
     * @param pUserId
     * @return List
     */
    @SuppressWarnings("unchecked")
    public List<RobotNodeStatusBean> getNodeStatusListByUser(Integer pUserId) {
        String hql = "select new com.itestor.iptv.modules.ne.vo.RobotNodeStatusBean(r,n) "
                + " from RobotManage r,RobotNetInfo n,RoleGroupRobot t,UserRoleGroup g "
                + " where r.idRobot=n.idRobot and t.idRobot=r.idRobot "
                + " and t.idRoleGroup=g.idRoleGroup and g.idUser=:userId order by r.name ";
        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("userId", pUserId);
        return query.list();
    }

    /**
     * 获取用户节点状态列表
     * @param pUserId
     * @return List
     */
    @SuppressWarnings("unchecked")
    public Map<Integer, RobotNodeStatusBean> getNodeStatusMapByUser(
            Integer pUserId) {
        String hql = "select new com.itestor.iptv.modules.ne.vo.RobotNodeStatusBean(r,n) "
                + " from RobotManage r,RobotNetInfo n,RoleGroupRobot t,UserRoleGroup g "
                + " where r.idRobot=n.idRobot and t.idRobot=r.idRobot "
                + " and t.idRoleGroup = g.idRoleGroup and g.idUser=:userId order by r.name ";

        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("userId", pUserId);

        Map<Integer, RobotNodeStatusBean> map = new HashMap<Integer, RobotNodeStatusBean>();

        List<RobotNodeStatusBean> list = query.list();
        for (RobotNodeStatusBean bean : list) {
            map.put(bean.getRobotManage().getIdRobot(), bean);
        }

        return map;
    }

    /**
     * 获取用户节点状态列表
     * @param idRobot
     * @param lastLogtime
     * @return IptvQuality
     */
    @SuppressWarnings("unchecked")
    public IptvQuality getNodeIptvQualityByIdRobot(Integer idRobot,
            long lastLogtime) {
        String hql = "select t " + " from RobotManage r,IptvQuality t "
                + " where t.idRobot=r.idRobot  "

                + " and t.logTime >:logTime and r.idRobot =:idRobot"
                + " order by r.name,t.logTime desc";

        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("logTime", lastLogtime);
        query.setParameter("idRobot", idRobot);

        query.setMaxResults(1);

        return (IptvQuality) query.uniqueResult();
    }

    /**
     * 获取用户节点状态列表
     * @param idRobot
     * @param lastLogtime
     * @return IptvQuality
     */
    @SuppressWarnings("unchecked")
    public IptvLostRate getNodeIptvLostRateByIdRobot(Integer idRobot,
            long lastLogtime) {
        String hql = "select t "
                + " from RobotManage r,IptvLostRate t "
                + " where t.idRobot=r.idRobot  and t.logTime >:logTime and r.idRobot =:idRobot"
                + " order by r.name,t.logTime desc";

        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("logTime", lastLogtime);
        query.setParameter("idRobot", idRobot);

        query.setMaxResults(1);
        return (IptvLostRate) query.uniqueResult();
    }

    /**
     * 获取用户节点告警列表
     * @param pUserId
     * @param pStartTime
     * @param pEndTime
     * @return Map
     */
    @SuppressWarnings("unchecked")
    public Map<Integer, RobotNodeStatusBean> getNodeAlertListByUser(
            Integer pUserId, Date pStartTime, Date pEndTime) {
        Map<Integer, RobotNodeStatusBean> alertNodeMap = new HashMap<Integer, RobotNodeStatusBean>();

        String hql = "select new com.itestor.iptv.modules.ne.vo.RobotNodeStatusBean(r,n) "
                + " from RobotLog l,RobotManage r,RobotNetInfo n,RoleGroupRobot t,UserRoleGroup g "
                + " where l.idRobot = r.idRobot and r.idRobot=n.idRobot and t.idRobot=r.idRobot "
                + " and t.idRoleGroup = g.idRoleGroup "
                + " and g.idUser=:userId and l.logTime >=:startTime and l.logTime <=:endTime ";

        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("userId", pUserId);

        query.setParameter("startTime", pStartTime.getTime()/1000);
        query.setParameter("endTime", pEndTime.getTime()/1000);

        List<RobotNodeStatusBean> list = query.list();

        for (RobotNodeStatusBean bean : list) {
            alertNodeMap.put(bean.getRobotManage().getIdRobot(), bean);
        }

        return alertNodeMap;
    }

    /** 
     * @param idRobot
     * @param pStartTime
     * @param pEndTime
     * @return Long
     */
    public Long getNodeAlertCountByIdRobot(
            Integer idRobot, Date pStartTime, Date pEndTime) {
        String hql = " select count(l.idRobotLog)"
                + " from RobotLog l"
                + " where l.idRobot =:idRobot and l.logTime >=:startTime and l.logTime <=:endTime ";

        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("idRobot", idRobot);

        query.setParameter("startTime", pStartTime.getTime() / 1000);
        query.setParameter("endTime", pEndTime.getTime() / 1000);
        
        Long alert_count = (Long) query.uniqueResult();
        
        return alert_count;
    }

    /**
     * 获取用户节点告警列表
     * @param pUserId
     * @param pStartTime
     * @param pEndTime
     * @param topn
     * @return List
     */
    @SuppressWarnings("unchecked")
    public List<RobotNodeStatusBean> getNodeStatusAlertsByUser(Integer pUserId,
            Date pStartTime, Date pEndTime, Integer topn) {

        String hql = "select new com.itestor.iptv.modules.ne.vo.RobotNodeStatusBean(r,n,l) "
                + " from RobotLog l,RobotManage r,RobotNetInfo n,RoleGroupRobot t,UserRoleGroup g "
                + " where l.idRobot = r.idRobot and r.idRobot=n.idRobot and t.idRobot=r.idRobot "
                + " and t.idRoleGroup = g.idRoleGroup "
                + " and g.idUser=:userId and l.logTime >=:startTime and l.logTime <=:endTime order by l.logTime desc,r.name ";

        Query query = getHibernateSession().createQuery(hql);
        query.setParameter("userId", pUserId);

        query.setParameter("startTime", pStartTime.getTime()/1000);
        query.setParameter("endTime", pEndTime.getTime()/1000);

        query.setMaxResults(topn);
        return query.list();
    }

}